package com.rxy.spring.convert;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.support.converter.MessageConversionException;
import org.springframework.amqp.support.converter.MessageConverter;

public class TextMessageConverter implements MessageConverter {

	//将一个java对象转换为Message
	@Override
	public Message toMessage(Object object, MessageProperties messageProperties) throws MessageConversionException {
		System.err.println("toMessage");
		return new Message(object.toString().getBytes(), messageProperties);
	}

	//将Message转换为Java对象
	@Override
	public Object fromMessage(Message message) throws MessageConversionException {
		System.err.println("fromMessage");
		//含有"text"的contentType，转换为字符串对象
		String contentType = message.getMessageProperties().getContentType();
		if(null != contentType && contentType.contains("text")) {
			return new String(message.getBody());
		}
		return message.getBody();
	}
}
